import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由懒加载
// import Login from "../components/login.vue"
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
// import Home from "../components/home.vue"
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
// import Welcome from "../components/welcome.vue"
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')


// import users from "../components/user/users.vue"
const users = () => import(/* webpackChunkName: "users_rights-roles" */ '../components/user/users.vue')
// import Rights from "../components/power/Rights.vue"
const Rights = () => import(/* webpackChunkName: "users_rights-roles" */ '../components/power/Rights.vue')
// import Roles from "../components/power/Roles.vue"
const Roles = () => import(/* webpackChunkName: "users_rights-roles" */ '../components/power/Roles.vue')



// import Cate from "../components/goods/Cate.vue"
const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
// import Params from "../components/goods/Params"
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')


// import List from "../components/goods/List.vue"
const List = () => import(/* webpackChunkName: "list_add" */ '../components/goods/List.vue')
// import Add from "../components/goods/add.vue"
const Add = () => import(/* webpackChunkName: "list_add" */ '../components/goods/add.vue')


// import Order from "../components/order/order.vue"
const Order = () => import(/* webpackChunkName: "Order_Reports" */ '../components/order/order.vue')
// import Reports from "../components/reports/reports.vue"
const Reports = () => import(/* webpackChunkName: "Order_Reports" */ '../components/reports/reports.vue')

Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: "/login" },
  {
    name: 'login',
    path: '/login',
    component: Login,

  },
  {
    //访问home默认重定向为welcome
    path: '/home',
    component: Home,
    //重定向
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      //用户页面路由
      {
        path: '/users',
        component: users
      },
      //权限列表路由
      {
        path: '/rights',
        component: Rights
      },
      //角色列表
      {
        path: '/roles',
        component: Roles
      },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      {
        path: "/goods", component: List
      },
      { path: '/goods/add', component: Add },
      {
        path: '/orders', component: Order
      },
      {
        path: '/reports', component: Reports
      }

    ]
  }
]

const router = new VueRouter({
  routes
})
//路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 来自那个
  //next方形
  if (to.path == '/login') return next()
  //获取token 如果没有token 强制返回到登录页面
  const tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) return next('/login')
  next()
})

export default router
